##############################################################
### Replication Code for STM Analysis                      ###
### Title: Relative Gains in the Shadow of a Trade War     ###
### Authors: Eddy S. F. Yeung and Kai Quek                 ###
### Version: December 6, 2021                              ###
##############################################################

### Set-up ###
## Clean the R environment and set the working directory
# RStudio version: 1.3.959
rm(list = ls())
setwd("~/Downloads/trade_replication") # change to your own working directory

## Load the required packages
library(stm)       # version 1.3.6
library(tm)        # version 0.7-8
library(stringi)   # version 1.7.6
library(extrafont) # version 0.17
# windowsFonts("Times" = windowsFont("Times")) # may be required for Windows users

## Import the dataset that contains all valid open-ended responses with party ID
data <- read.csv("stm_dataset.csv", na.strings = c("", "NA"))

## Process the text data
processed <- textProcessor(data$open_ended, metadata = data)
out <- prepDocuments(processed$documents, processed$vocab, processed$meta, lower.thresh = 2)
docs <- out$documents
vocab <- out$vocab
meta <- out$meta

## Run the STM (estimating a five-topic STM)
set.seed(1)
selectedmodel <- stm(out$documents, out$vocab, K = 5,
                     prevalence =~ treatment * republican,
                     data = out$meta, init.type = "Spectral")

## Display the words that characterize each topic (related: Table A5)
labelTopics(selectedmodel)

## Display representative responses from each topic (related: Table A6)
# Topic 1
thoughts1 <- findThoughts(selectedmodel, 
                          texts = meta$open_ended, 
                          n = 7, 
                          topics = 1)$docs[[1]]
plotQuote(thoughts1, width = 100, main = "Topic 1")

# Topic 2
thoughts2 <- findThoughts(selectedmodel, 
                          texts = meta$open_ended, 
                          n = 7, 
                          topics = 2)$docs[[1]]
thoughts2 <- stri_enc_toutf8(thoughts2, TRUE)
plotQuote(thoughts2, width = 100, main = "Topic 2")

# Topic 3
thoughts3 <- findThoughts(selectedmodel, 
                          texts = meta$open_ended, 
                          n = 7, 
                          topics = 3)$docs[[1]]
plotQuote(thoughts3, width = 100, main = "Topic 3")

# Topic 4
thoughts4 <- findThoughts(selectedmodel, 
                          texts = meta$open_ended, 
                          n = 7, 
                          topics = 4)$docs[[1]]
thoughts4 <- stri_enc_toutf8(thoughts4, TRUE)
plotQuote(thoughts4, width = 100, main = "Topic 4")

# Topic 5
thoughts5 <- findThoughts(selectedmodel, 
                          texts = meta$open_ended, 
                          n = 7, 
                          topics = 5)$docs[[1]]
plotQuote(thoughts5, width = 100, main = "Topic 5")

## Plot the heterogeneous treatment effects (Figures A9 and A10)
# Topic 1
set.seed(1)
prep <- estimateEffect(c(1) ~ treatment * republican, selectedmodel,
                       metadata = out$meta,
                       uncertainty = "None")
pdf(file = "~/Downloads/trade_replication/stm_topic-1.pdf")
par(family = "Times", ps = 14)
plot.estimateEffect(prep,
                    covariate = "republican",
                    model = selectedmodel,
                    method = "continuous",
                    xlab = "Party Identification (Strong Democrat = 0, Strong Republican = 4)",
                    moderator = "treatment",
                    moderator.value = 0,
                    linecol = "blue",
                    ylim = c(0.0,0.4),
                    printlegend = F)
plot.estimateEffect(prep,
                    covariate = "republican",
                    model = selectedmodel,
                    method = "continuous",
                    moderator = "treatment",
                    moderator.value = 1,
                    linecol = "red",
                    ylim = c(0.0,0.4),
                    add = T,
                    printlegend = F)
legend("topright", c("No Additional Vignette", "Additional Vignette"), 
       lwd = 2, col = c("blue", "red"))
dev.off()

# Topic 2
set.seed(1)
prep <- estimateEffect(c(2) ~ treatment * republican,
                       selectedmodel,
                       metadata = out$meta,
                       uncertainty = "None")
pdf(file = "~/Downloads/trade_replication/stm_topic-2.pdf")
par(family = "Times", ps = 14)
plot.estimateEffect(prep,
                    covariate = "republican",
                    model = selectedmodel,
                    method = "continuous",
                    xlab = "Party Identification (Strong Democrat = 0, Strong Republican = 4)",
                    moderator = "treatment",
                    moderator.value = 0,
                    linecol = "blue",
                    ylim = c(0.0,0.4),
                    printlegend = F)
plot.estimateEffect(prep,
                    covariate = "republican",
                    model = selectedmodel,
                    method = "continuous",
                    moderator = "treatment",
                    moderator.value = 1,
                    linecol = "red",
                    ylim = c(0.0,0.4),
                    add = T,
                    printlegend = F)
legend("topright", c("No Additional Vignette", "Additional Vignette"), 
       lwd = 2, col = c("blue", "red"))
dev.off()

# Topic 3
set.seed(1)
prep <- estimateEffect(c(3) ~ treatment * republican,
                       selectedmodel,
                       metadata = out$meta,
                       uncertainty = "None")
pdf(file = "~/Downloads/trade_replication/stm_topic-3.pdf")
par(family = "Times", ps = 14)
plot.estimateEffect(prep,
                    covariate = "republican",
                    model = selectedmodel,
                    method = "continuous",
                    xlab = "Party Identification (Strong Democrat = 0, Strong Republican = 4)",
                    moderator = "treatment",
                    moderator.value = 0,
                    linecol = "blue",
                    ylim = c(0.0,0.4),
                    printlegend = F)
plot.estimateEffect(prep,
                    covariate = "republican",
                    model = selectedmodel,
                    method = "continuous",
                    moderator = "treatment",
                    moderator.value = 1,
                    linecol = "red",
                    ylim = c(0.0,0.4),
                    add = T,
                    printlegend = F)
legend("topright", c("No Additional Vignette", "Additional Vignette"), 
       lwd = 2, col = c("blue", "red"))
dev.off()

# Figure 4
set.seed(1)
prep <- estimateEffect(c(4) ~ treatment * republican,
                       selectedmodel,
                       metadata = out$meta,
                       uncertainty = "None")
pdf(file = "~/Downloads/trade_replication/stm_topic-4.pdf")
par(family = "Times", ps = 14)
plot.estimateEffect(prep,
                    covariate = "republican",
                    model = selectedmodel,
                    method = "continuous",
                    xlab = "Party Identification (Strong Democrat = 0, Strong Republican = 4)",
                    moderator = "treatment",
                    moderator.value = 0,
                    linecol = "blue",
                    ylim = c(0.0,0.4),
                    printlegend = F)
plot.estimateEffect(prep,
                    covariate = "republican",
                    model = selectedmodel,
                    method = "continuous",
                    moderator = "treatment",
                    moderator.value = 1,
                    linecol = "red",
                    ylim = c(0.0,0.4),
                    add = T,
                    printlegend = F)
legend("topright", c("No Additional Vignette", "Additional Vignette"), 
       lwd = 2, col = c("blue", "red"))
dev.off()

# Figure 5
set.seed(1)
prep <- estimateEffect(c(5) ~ treatment * republican,
                       selectedmodel,
                       metadata = out$meta,
                       uncertainty = "None")
pdf(file = "~/Downloads/trade_replication/stm_topic-5.pdf")
par(family = "Times", ps = 14)
plot.estimateEffect(prep,
                    covariate = "republican",
                    model = selectedmodel,
                    method = "continuous",
                    xlab = "Party Identification (Strong Democrat = 0, Strong Republican = 4)",
                    moderator = "treatment",
                    moderator.value = 0,
                    linecol = "blue",
                    ylim = c(0.15,0.37),
                    printlegend = F)
plot.estimateEffect(prep,
                    covariate = "republican",
                    model = selectedmodel,
                    method = "continuous",
                    moderator = "treatment",
                    moderator.value = 1,
                    linecol = "red",
                    ylim = c(0.15,0.37),
                    add = T,
                    printlegend = F)
legend("topright", c("No Additional Vignette", "Additional Vignette"), 
       lwd = 2, col = c("blue", "red"))
dev.off()
